package junior.util.address;

import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.tuple.Pair;
import org.springframework.util.CollectionUtils;

public class Station {
	
	private static final Pattern ROAD_NUM = Pattern.compile("\\d+");
	
	/** 单号门牌 */
	private static final Integer TYPE_ODD = 1;
	/** 双号门牌 */
	private static final Integer TYPE_EVEN = 2;
	
	private int fMin;
	
	private int fMax;
	
	private int fType;
	
	private long divisionId;
	
	private long stationId;
	
	private long lineId;
	
	/**
	 * 根据数组的<b>最后</b>6列来实例化
	 * 
	 * @param records 6列分别是：fMin, fMax, fType, divisionId, stationId, lineId
	 * 
	 * @see {@link #Station(int, int, int, long)}
	 * @author ZhangXiaoye
	 * @date 2016年11月19日 下午3:47:36
	 */
	public Station(String[] records){
		if(records.length < 6){
			throw new IllegalArgumentException("数组长度不足：" + Arrays.toString(records));
		}
		int start = records.length - 6;
		fMin = Integer.parseInt(records[start++]);
		fMax = Integer.parseInt(records[start++]);
		fType = Integer.parseInt(records[start++]);
		divisionId = Long.parseLong(records[start++]);
		stationId = Long.parseLong(records[start++]);
		lineId = Long.parseLong(records[start++]);
	}
	
	/**
	 * @param fMin 门牌号最小值
	 * @param fMax 门牌号最大值
	 * @param fType 门牌号单双类型 0不区分 1单号 2双号
	 * @param stationId 站点id
	 * @author ZhangXiaoye
	 * @date 2016年11月19日 下午3:46:46
	 */
	public Station(int fMin, int fMax, int fType, long stationId){
		this.fMin = fMin;
		this.fMax = fMax;
		this.fType = fType;
		this.stationId = stationId;
	}
	
	/**
	 * 判断是否匹配门牌号
	 * 
	 * @param roadNumber 门牌号
	 * @return
	 * @author ZhangXiaoye
	 * @date 2016年11月19日 下午3:49:03
	 */
	public boolean match(String roadNumber){
		Matcher m = ROAD_NUM.matcher(roadNumber);
		if(m.find()){
			int n = Integer.parseInt(m.group());
			if(TYPE_ODD.equals(fType) && n % 2 == 0){
				return false;
			}
			if(TYPE_EVEN.equals(fType) && n % 2 == 1){
				return false;
			}
			return n >= fMin && n <= fMax;
		}else{
			return false;
		}
	}
	
	public static Station match(Pair<Address, ? extends List<Pair<Address, Station>>> all){
		Address addr = all.getLeft();
		List<Pair<Address, Station>> stations = all.getRight();
		if(CollectionUtils.isEmpty(stations)){
			return null;
		}
		return null;
	}

	public int getfMin() {
		return fMin;
	}

	public void setfMin(int fMin) {
		this.fMin = fMin;
	}

	public int getfMax() {
		return fMax;
	}

	public void setfMax(int fMax) {
		this.fMax = fMax;
	}

	public int getfType() {
		return fType;
	}

	public void setfType(int fType) {
		this.fType = fType;
	}

	public long getDivisionId() {
		return divisionId;
	}

	public void setDivisionId(long divisionId) {
		this.divisionId = divisionId;
	}

	public long getStationId() {
		return stationId;
	}

	public void setStationId(long stationId) {
		this.stationId = stationId;
	}

	public long getLineId() {
		return lineId;
	}

	public void setLineId(long lineId) {
		this.lineId = lineId;
	}

	@Override
	public String toString() {
		return String.format("Station [fMin=%s, fMax=%s, fType=%s, stationId=%s]", fMin, fMax, fType, stationId);
	}

}
